package com.chengyu.core.controller.callback.pay;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.chengyu.core.controller.callback.PayCallbackController;
import com.chengyu.core.controller.callback.logic.RechargeLogic;
import com.chengyu.core.controller.callback.manager.CallbackManager;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.enums.AccountEnums;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.CommonPayOrderMapper;
import com.chengyu.core.model.CommonPayOrder;
import com.chengyu.core.model.CommonPayOrderExample;
import com.chengyu.core.model.UmsMemberRecharge;
import com.chengyu.core.service.funds.MemberAccountLogService;
import com.chengyu.core.service.member.MemberRechargeService;
import com.chengyu.core.service.member.MemberService;
import com.chengyu.core.service.pay.PayService;
import com.chengyu.core.service.pay.TyPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;



@Log4j2
@Api(tags = "订单支付回调")
@Controller
public class OrderPayCallbackController extends PayCallbackController {



    @Autowired
    private MemberRechargeService rechargeService;

    @Autowired
    private CommonPayOrderMapper payOrderMapper;


    @Autowired
    private MemberAccountLogService memberAccountLogService;


    @Autowired
    private MemberService memberService;


    @ApiOperation(value = "微信支付回调 /商城")
    @ResponseBody
    @RequestMapping(value = PayService.WXPAY_NOTIFY_URL_ORDER, method = RequestMethod.POST)
    public CommonResult<String> paySusForWx(HttpServletRequest request) throws Exception {
        return this.paySusForWx(request, CallbackManager.CALLBACK_ORDER);
    }
    @ApiOperation(value = "拖运微信支付回调 /运输")
    @ResponseBody
    @RequestMapping(value = PayService.TY_WXPAY_NOTIFY_URL_ORDER, method = RequestMethod.POST)
    public CommonResult<String> tyPaySusForWx(HttpServletRequest request) throws Exception {
        return this.paySusForWx(request, CallbackManager.TY_CALLBACK_ORDER);
    }


    @ApiOperation(value = "拖运支付宝支付回调 /运输")
    @ResponseBody
    @RequestMapping(value = PayService.TY_ALIPAY_NOTIFY_URL_ORDER, method = RequestMethod.POST)
    public CommonResult<String> tyPaySusForZfb(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return this.paySusForZfb(request, response, CallbackManager.TY_CALLBACK_ORDER);
    }


//    @ApiOperation(value = "拖运微信支付回调 /运输")
//    @ResponseBody
//    @RequestMapping(value = PayService.TY_ADD_WXPAY_NOTIFY_URL_ORDER, method = RequestMethod.POST)
//    public CommonResult<String> tyPaySusForWxAdd(HttpServletRequest request) throws Exception {
//        return this.paySusForWx(request, CallbackManager.TY_CALLBACK_ORDER_ADD);
//    }
//
//
//    @ApiOperation(value = "拖运支付宝支付回调 /运输")
//    @ResponseBody
//    @RequestMapping(value = PayService.TY_ADD_ALIPAY_NOTIFY_URL_ORDER, method = RequestMethod.POST)
//    public CommonResult<String> tyPaySusForZfbAdd(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        return this.paySusForZfb(request, response, CallbackManager.TY_CALLBACK_ORDER_ADD);
//    }


    @ApiOperation(value = "充值微信支付回调 /充值/打赏/保证金")
    @ResponseBody
    @RequestMapping(value=PayService.WXPAY_NOTIFY_URL_RECHARGE, method=RequestMethod.POST)
    public CommonResult<String> reChangePaySusForWx(HttpServletRequest request) throws Exception{
        return this.paySusForWx(request, CallbackManager.CALLBACK_RECHARGE);
    }


//    @ApiOperation(value = "充值微信支付回调 /充值/打赏/保证金")
//    @ResponseBody
//    @RequestMapping(value="/common/recharge/wxPay/test", method = RequestMethod.GET)
//    public com.chengyu.core.result.CommonResult<Object> updateShowStatus() throws ServiceException {
//         paySus("R1726806753645239");
//        return null;
//    }
//
//
//    public void paySus(String orderNo) throws ServiceException {
//        UmsMemberRecharge recharge = rechargeService.getRechargeLogByOrderNo(orderNo);
//
//
//        CommonPayOrderExample payOrderExample = new CommonPayOrderExample();
////		payOrderExample.createCriteria().andPayOrderNoEqualTo(orderNo).andStatusEqualTo(CommonConstant.WAIT_INT);
//        payOrderExample.createCriteria().andOrderSnEqualTo(orderNo).andStatusEqualTo(CommonConstant.WAIT_INT);
//        List<CommonPayOrder> payOrderList = payOrderMapper.selectByExample(payOrderExample);
//        if(CollectionUtil.isNotEmpty(payOrderList)) {
//            CommonPayOrder updatePayOrder = new CommonPayOrder();
//            updatePayOrder.setId(payOrderList.get(0).getId());
//            updatePayOrder.setStatus(CommonConstant.SUS_INT);
//            updatePayOrder.setUpdTime(DateUtil.date());
//            updatePayOrder.setPayTime(updatePayOrder.getUpdTime());
//            payOrderMapper.updateByPrimaryKeySelective(updatePayOrder);
//
//            log.info("recharge.getStatus():{}",recharge.getStatus());
//            //支付成功, 更新状态
//            log.info("CommonConstant.WAIT:{}",CommonConstant.WAIT);
//            if(Objects.equals(recharge.getStatus(), CommonConstant.WAIT)){
//                log.info("ininin:{}",recharge.getType());
//                if(recharge.getType() == AccountEnums.AccountMethod.BALANCE.getValue() ){
//                    log.info("ininin:{1}");
//                    rechargeService.rechargSusByCallback(recharge);
//                }else 	if(recharge.getType().equals(AccountEnums.AccountMethod.BZJ.getValue())){
//                    log.info("ininin:{2}");
//                    rechargeService.rechargBzjSusByCallback(recharge);
//                }
//            }else{
//
//            }
////            logger.info("解密后的明文:{}",plainBody);
//
//        }
//
//
//    }
//



    @ApiOperation(value = "支付宝支付回调 /商城")
    @ResponseBody
    @RequestMapping(value = PayService.ALIPAY_NOTIFY_URL_ORDER, method = RequestMethod.POST)
    public CommonResult<String> paySusForZfb(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return this.paySusForZfb(request, response, CallbackManager.CALLBACK_ORDER);
    }


    @ApiOperation(value = "充值支付宝支付回调 /充值/打赏/保证金")
    @ResponseBody
    @RequestMapping(value= PayService.ALIPAY_NOTIFY_URL_RECHARGE, method=RequestMethod.POST)
    public CommonResult<String> reChangePaySusForZfb(HttpServletRequest request, HttpServletResponse response) throws Exception{
        return this.paySusForZfb(request, response, CallbackManager.CALLBACK_RECHARGE);
    }




}
